import { ref, computed, watch } from 'vue'
import type { DetectRequest, DetectResult, FilterStatus } from '../types/ssh'
import { getStatusText, getStatusClass } from '../utils/ssh'

export class SSHDetectorModel {
  // 表单数据
  public formData = ref<DetectRequest>({
    network: '',
    port: 22,
    username: '',
    password: ''
  })

  // 结果数据
  public results = ref<DetectResult[]>([])
  public loading = ref(false)
  public error = ref('')
  public filterStatus = ref<FilterStatus>('all')

  // 进度相关
  public progress = ref(0)
  public current = ref(0)
  public total = ref(0)
  public progressMessage = ref('')

  // 分页相关
  public currentPage = ref(1)
  public pageSize = ref(10)

  // 计算属性
  public filteredResults = computed(() => {
    if (this.filterStatus.value === 'all') {
      return this.results.value
    }
    return this.results.value.filter(result => {
      if (this.filterStatus.value === 'port_closed') {
        return !result.port_open
      }
      if (this.filterStatus.value === 'auth_failed') {
        return result.status === 'auth_failed'
      }
      if (this.filterStatus.value === 'connected') {
        return result.status === 'connected'
      }
      if (this.filterStatus.value === 'sudo_supported') {
        return result.status === 'sudo_supported'
      }
      return false
    })
  })

  public totalPages = computed(() => {
    return Math.ceil(this.filteredResults.value.length / this.pageSize.value)
  })

  public paginatedResults = computed(() => {
    const start = (this.currentPage.value - 1) * this.pageSize.value
    const end = start + this.pageSize.value
    return this.filteredResults.value.slice(start, end)
  })

  public displayedPages = computed(() => {
    const pages = []
    const maxPages = 7
    const halfMax = Math.floor(maxPages / 2)

    if (this.totalPages.value <= maxPages) {
      for (let i = 1; i <= this.totalPages.value; i++) {
        pages.push(i)
      }
    } else {
      if (this.currentPage.value <= halfMax + 1) {
        for (let i = 1; i <= maxPages - 1; i++) {
          pages.push(i)
        }
        pages.push('...')
        pages.push(this.totalPages.value)
      } else if (this.currentPage.value >= this.totalPages.value - halfMax) {
        pages.push(1)
        pages.push('...')
        for (let i = this.totalPages.value - maxPages + 2; i <= this.totalPages.value; i++) {
          pages.push(i)
        }
      } else {
        pages.push(1)
        pages.push('...')
        for (let i = this.currentPage.value - halfMax; i <= this.currentPage.value + halfMax; i++) {
          pages.push(i)
        }
        pages.push('...')
        pages.push(this.totalPages.value)
      }
    }

    return pages
  })

  // 方法
  public handlePageChange(page: number | string) {
    if (typeof page === 'number') {
      this.currentPage.value = page
    }
  }

  public handlePageSizeChange(event: Event) {
    const select = event.target as HTMLSelectElement
    this.pageSize.value = parseInt(select.value)
    this.currentPage.value = 1
  }

  public clearResults() {
    this.results.value = []
  }

  constructor() {
    watch(this.filterStatus, () => {
      this.currentPage.value = 1
    })
  }
}